package com.songcy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pro.bean.Info;
import com.pro.bean.Mark;
import com.songcy.dao.GetSameNameInfoDao;

public class GetSameNameInfoServlet extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
		System.out.println("aNI"+name);
		
		//System.out.println("name"+name);
		GetSameNameInfoDao gs = new GetSameNameInfoDao();
		List rs = gs.getinfos(name);
		//System.out.println("GetSameNameInfoServlet-rs:"+rs.size());
		List markrs = new ArrayList();
		Iterator it = rs.iterator();
		while(it.hasNext())
		{
			Info i = (Info)it.next();
			//System.out.println("cardid:"+i.getCardid());
			List tmp = gs.find(i.getCardid(),new Mark());
			//System.out.println("tmpsize:"+tmp.size());
			markrs.add(tmp.get(0));
		}
		//HttpSession session=request.getSession();
		request.setAttribute("rs", rs);
		//System.out.println("GetSameNameInfoServlet-rs.size"+rs.size());
		request.setAttribute("markrs", markrs);
		request.getRequestDispatcher("../gxr/admin_same_right.jsp").forward(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
